package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 单张扑克牌的类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Card {
    /**
     * 卡牌花色
     */
    String suit;
    /**
     * 卡牌点数
     */
    char rank;

    /**
     * 根据卡牌点数赋予花色
     *
     * @param i    卡牌点数
     * @param card 一副卡牌
     * @param suit 花色
     */
    public void suit(int i, List<Card> cards, String suit) {
        if (i < 11 && i > 1) {
            cards.add(new Card(suit, (char) i));
        } else {
            switch (i) {
                case 11:
                    cards.add(new Card(suit, 'J'));
                    break;
                case 12:
                    cards.add(new Card(suit, 'Q'));
                    break;
                case 13:
                    cards.add(new Card(suit, 'K'));
                    break;
                case 1:
                    cards.add(new Card(suit, 'A'));
                    break;
            }
        }
    }

    /**
     * 根据需要几幅牌组合成一套牌
     *
     * @param pack 需要扑克牌副数量
     */
    public List<Card> pack(int pack) {
        List<Card> cards = new ArrayList<>();
        for (int x = 0; x < pack; x++) {
            for (int i = 1; i < 14; i++) {
                for (int j = 0; j < 4; j++) {
                    switch (j) {
                        case 0:
                            suit(i, cards, "spades");
                            break;
                        case 1:
                            suit(i, cards, "hearts");
                            break;
                        case 2:
                            suit(i, cards, "diamonds");
                            break;
                        case 3:
                            suit(i, cards, "clubs");
                            break;
                    }
                }
            }
        }
        Collections.shuffle(cards);
        return cards;
    }


    /**
     * 百家乐单局牌发牌
     *
     * @param baccaratList 一套卡牌
     */
    public List<Poker> comparePoint() {
        List<Card> baccaratList = pack(8);
        int i = 1;
        List<Poker> pokers = new ArrayList<>();
        Poker pokerGame = new Poker();
        for (Card baccarat : baccaratList) {
            switch (i) {
                case 1:
                case 3:
                    pokerGame.getPlayer().add(baccarat);
                    i++;
                    break;
                case 2:
                case 4:
                    pokerGame.getBanker().add(baccarat);
                    i++;
                    break;
                case 5:
                    pokerGame.countPlayer();
                    pokerGame.countBanker();
                    if (pokerGame.getBankerPoint() == 8 || pokerGame.getBankerPoint() == 9 || pokerGame.getPlayerPoint() == 8 || pokerGame.getPlayerPoint() == 9) {
                        Poker temp = new Poker();
                        BeanUtils.copyProperties(pokerGame, temp);
                        temp.win();
                        pokers.add(temp);
                        pokerGame = new Poker();
                        i = 1;
                    } else if (pokerGame.getPlayerPoint() < 6) {
                        pokerGame.getPlayer().add(baccarat);
                        i++;
                    } else {
                        i++;
                    }
                    break;
                case 6:
                    if (pokerGame.getPlayer().size() < 3) {
                        if (pokerGame.getBankerPoint() != 6 && pokerGame.getBankerPoint() != 7) {
                            pokerGame.getBanker().add(baccarat);
                        }
                    } else {
                        char point;
                        switch (pokerGame.getBankerPoint()) {
                            case 0:
                            case 1:
                            case 2:
                                pokerGame.getBanker().add(baccarat);
                                break;
                            case 3:
                                if (pokerGame.getPlayer().get(2).getRank() != '8') {
                                    pokerGame.getBanker().add(baccarat);
                                }
                                break;
                            case 4:
                                point = pokerGame.getPlayer().get(2).getRank();
                                if (point == '2' || point == '3' || point == '4' || point == '5' || point == '6' || point == '7') {
                                    pokerGame.getBanker().add(baccarat);
                                }
                                break;
                            case 5:
                                point = pokerGame.getPlayer().get(2).getRank();
                                if (point == '4' || point == '5' || point == '6' || point == '7') {
                                    pokerGame.getBanker().add(baccarat);
                                }
                                break;
                            case 6:
                                point = pokerGame.getPlayer().get(2).getRank();
                                if (point == '6' || point == '7') {
                                    pokerGame.getBanker().add(baccarat);
                                }
                                break;
                        }
                    }
                    Poker temp = new Poker();
                    pokerGame.countPlayer();
                    pokerGame.countBanker();
                    BeanUtils.copyProperties(pokerGame, temp);
                    temp.win();
                    pokers.add(temp);
                    pokerGame = new Poker();
                    i = 1;
                    break;
            }
        }
        return pokers;
    }
}
